import { Body, Controller, Delete, Get, HttpException, HttpStatus, Param, Patch, Post, Query } from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
import { EEnableStatus } from '@/shared/types/globalEnum'
import { CareerService } from './career.service'
import { CreateCareerDto, ListPageCareerDto, UpdateCareerDto, WebListPageCareerDto } from './dto/career.dto'

@ApiTags('后台/职业配置')
@Controller('admin/career')
export class CareerController {
  constructor(private readonly careerService: CareerService) {}

  /**
   * 职业配置列表
   */
  @Get()
  list(
    @Query()
    query: ListPageCareerDto,
  ) {
    return this.careerService.findPage(query)
  }

  /**
   * 职业配置详情
   */
  @Get(':careerId')
  detail(
    @Param('careerId')
    careerId: number,
  ) {
    return this.careerService.findOne({
      careerId,
    })
  }

  /**
   * 创建职业配置
   */
  @Post()
  create(@Body() data: CreateCareerDto) {
    return this.careerService.create(data)
  }

  /**
   * 更新职业配置
   */
  @Patch(':careerId')
  async update(
    @Param('careerId')
    careerId: number,
    @Body()
    data: UpdateCareerDto,
  ) {
    const findData = await this.careerService.findOne({
      careerId,
    })
    if (!findData) {
      throw new HttpException('职业配置不存在', HttpStatus.NOT_FOUND)
    }
    await this.careerService.update(careerId, data)
  }

  /**
   * 删除职业配置
   */
  @Delete(':careerId')
  async remove(@Param('careerId') careerId: number) {
    const findData = await this.careerService.findOne({
      careerId,
    })
    if (!findData) {
      throw new HttpException('职业配置不存在', HttpStatus.NOT_FOUND)
    }
    await this.careerService.remove(careerId)
  }
}

@ApiTags('官网/职业配置')
@Controller('web/career')
export class WebCareerController {
  constructor(private readonly careerService: CareerService) {}
  /**
   * 启用的的职业配置列表
   */
  @Get()
  webList(
    @Query()
    query: WebListPageCareerDto,
  ) {
    return this.careerService.findPage({
      visibleStatus: EEnableStatus.ENABLED,
      ...query,
    })
  }
}
